package saxion.speelveld.spaceengineer;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import saxion.speelveld.spaceengineer.controller.BitmapManager;
import saxion.speelveld.spaceengineer.controller.BuildingRowAdapter;
import saxion.speelveld.spaceengineer.database.AchievementDAO;
import saxion.speelveld.spaceengineer.model.Achievement;
import saxion.speelveld.spaceengineer.model.Building;
import saxion.speelveld.spaceengineer.model.Resource;
import saxion.speelveld.spaceengineer.model.ResourceWrapper;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class BuildingScreen extends Activity implements Observer
{

	private int lastSelected = -1;

	private AchievementDAO achievementDatasource;

	private ArrayList<Building> buildingList;

	private ResourceWrapper resourceArray;

	// private Game game;

	private TableRow topResourceBar;

	private TableRow topResourceBar2;

	private TableRow topResourceBar3;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_building_screen);

		// Log.d("BuildingScreen", "oncreate start");

		achievementDatasource = new AchievementDAO(getApplicationContext());

		topResourceBar = (TableRow) findViewById(R.id.trBuildingTopResources);
		topResourceBar2 = (TableRow) findViewById(R.id.trBuildingTopResources2);
		topResourceBar3 = (TableRow) findViewById(R.id.trBuildingTopResources3);
		ListView listview = (ListView) findViewById(R.id.list);

		Object objResources = getIntent().getSerializableExtra("GameScreen.Resources");
		if (objResources != null)
		{
			resourceArray = (ResourceWrapper) objResources;
		}

		setResourcesInHeader();

		buildingList = Building.GetAllDifferentBuildings();

		BuildingRowAdapter adapter = new BuildingRowAdapter(this, buildingList, resourceArray);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new BuildingListClickListener());

		achievementDatasource.open();
		Achievement achie = achievementDatasource.getAchievement(7);
		if (achie.isAccomplished() == false)
		{
			Intent showAchievementPopup = new Intent(this.getApplicationContext(), CustomAchievementPopup.class);
			showAchievementPopup.putExtra("saxion.speelveld.spaceengineer.achievement", achie);
			startActivity(showAchievementPopup);
		}
		achievementDatasource.close();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.building_screen, menu);
		return true;
	}

	private class BuildingListClickListener implements OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			boolean hasEnoughResources = true;

			ArrayList<Resource> buildingCosts = buildingList.get(arg2).resourceCosts;
			for (Resource res : buildingCosts)
			{
				if (resourceArray.getResourceAmount(res.getResourceType()) < res.getAmount())
				{
					hasEnoughResources = false;
				}
			}

			if (hasEnoughResources == true)
			{
				lastSelected = arg2;
				finish();
			}
			else
			{
				Toast.makeText(getApplicationContext(),
					"Not enough resources available for " + buildingList.get(arg2).getBuildingName(),
					Toast.LENGTH_SHORT).show();
			}

		}
	}

	private void setResourcesInHeader()
	{
		if (topResourceBar.getChildCount() > 0 && topResourceBar2.getChildCount() > 0
			&& topResourceBar3.getChildCount() > 0)
		{
			topResourceBar.removeAllViews();
			topResourceBar2.removeAllViews();
			topResourceBar3.removeAllViews();
		}
		Bitmap img = null;
		TextView tvTemp = null;
		int count = 0;
		int arrayLength = resourceArray.getResourcesArray().length;
		for (Resource res : resourceArray.getResourcesArray())
		{
			img = BitmapManager.getBitmapByResourceType(res.getResourceType(),
				getApplicationContext(), 16, 16);
			ImageView imageview = new ImageView(getApplicationContext());
			imageview.setImageBitmap(img);
			imageview.setMaxHeight(16);
			imageview.setPadding(14, 8, 4, 0);

			tvTemp = new TextView(getApplicationContext());
			tvTemp.setText(res.getAmount() + " ");
			tvTemp.setTextColor(Color.BLACK);

			if (count <= (arrayLength / 3))
			{
				topResourceBar.addView(imageview);
				topResourceBar.addView(tvTemp);
			}
			else if (count <= (((arrayLength / 3) * 2) + 1))
			{
				topResourceBar2.addView(imageview);
				topResourceBar2.addView(tvTemp);
			}
			else
			{
				topResourceBar3.addView(imageview);
				topResourceBar3.addView(tvTemp);
			}
			count = count + 1;
		}
	}

	@Override
	public void finish()
	{
		if (lastSelected >= 0)
		{
			String inputBuildingId = "saxion.speelveld.spaceengineer.buildingscreen.buildingid";
			String inputBuildingClass = "saxion.speelveld.spaceengineer.buildingscreen.buildingclass";
			Intent data = new Intent();
			data.putExtra(inputBuildingId, lastSelected);
			data.putExtra(inputBuildingClass, buildingList.get(lastSelected));
			setResult(RESULT_OK, data);
		}
		else
		{
			setResult(RESULT_CANCELED);
		}
		super.finish();
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		Log.d("BuildingSreen", "Update called.");
		setResourcesInHeader();
	}
}
